/**
 * Created by Brant on 2016/12/28.
 */
module.exports = function ($scope, $state, $cookies,$rootScope,newsService,httpservice) {
  $rootScope.showCaseDiv = false;
    /*登录用户ID*/
    var receiveUserId = kbt.decrypt($cookies.get('cookieUserId'));
    /*取出当前用户名字*/
    $scope.userPortraitName = $cookies.get('userPortraitName');
    /*初始页*/
    $scope.pageNum = 1;
    /*初始页*/
    $scope.pageNum2 = 1;
    /*每页显示的条数*/
    $scope.pageSize = 10;
    /*系统消息与动态消息标志:1为系统;2为动态*/
    $scope.type = 2;

    $scope.records = [];

    $scope.system = [];


  $scope.mIngUr=httpservice.murlWebHttp;


    /**
     * 初始化
     */
    $scope.init = function () {
        // $("#id-data-null").hide();
        $("#yb-message-list1").click(function () {
            $('.news-con1').show();
            $('.news-con2').hide();
            $("#yb-list1").show();
            $("#yb-list2").hide();
            var target = $("#yb-message-list1");
            var which = parseInt(target.val());
            target.addClass("active");
            target.siblings().removeClass("active");
            $('[id*="tab-"]').addClass('hide');
            $('#tab-' + which).removeClass('hide');

            $("#dong-bottom-page").show();
            $("#sys-bottom-page").hide();
        });
        $("#yb-message-list2").click(function () {
            $('.news-con1').hide();
            $('.news-con2').show();
            $("#yb-list1").hide();
            $("#yb-list2").show();
            var target = $("#yb-message-list2");
            var which = parseInt(target.val());
            target.addClass("active");
            target.siblings().removeClass("active");
            $('[id*="tab-"]').addClass('hide');
            $('#tab-' + which).removeClass('hide');

            $("#dong-bottom-page").hide();
            $("#sys-bottom-page").show();
        });
      $('#new02').hide();



        $scope.queryMessage();

        $scope.getEvidenceTypeList();

        if($('#yb-message-list1').hasClass('active')){
            $("#dong-bottom-page").show();
            $("#sys-bottom-page").hide();
        }
    };


    /**
     * 查询动态消息
     * @param currentPage 当前页
     */
    $scope.queryMessage = function (currentPage) {
      // $('#yb-list1').children().empty();
        // $('#id-data-null').hide();
        $('.news-con1').show();
        $('.news-con2').hide();
        $('#yb-list1').show();
        // $('L_newsShowMore1').show();
        $('#yb-list2').hide();
        // $('L_newsShowMore2').hide();
      // $scope.records = [];
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        newsService.queryMessage("", receiveUserId, "", currentPage, $scope.pageSize).then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            if (data.code == 1) {
                $('.select').css('color', '#666');
              $scope.records=[];
                $scope.records = $scope.records.concat(data.data);
               /* if($scope.records.length<10){
                  $('.L_newsShowMore1').hide();
                  $('.L_newsShowMore2').hide();
                }else if($scope.records.length>=10){
                  $('.L_newsShowMore1').show();
                  $('.L_newsShowMore2').show();
                }*/
                $scope.changeAfter(data.data);
                $scope.totalPage = data.totalPage;//总页数
                $scope.totalCount = data.totalCount;//总条数
                $scope.beginPage = data.beginPage;//开始页
                $scope.endPage = data.endPage;//结束页
                if(!$scope.records.length){
                  $('#id-data-null1').css('display','block');
                  $('#id-data-null2').css('display','block');
                  /*$('#yb-list1').css('display','none');
                  $('#yb-list2').css('display','none');*/
                }else {
                  $('#id-data-null1').css('display','none');
                  $('#id-data-null2').css('display','none');
                  /*$('#yb-list1').css('display','block');
                  $('#yb-list2').css('display','block');*/
                }
                /*如果数据为空，怎隐藏底部页码*/
                if ($scope.totalCount > 0) {
                    $('#dong-bottom-page').show();
                }else{
                    $('#dong-bottom-page').hide();
                }
                /*声明一个数组显示页数*/
                $scope.arrPage = new Array();
                for (var j = 0; j < $scope.endPage; j++) {
                    $scope.arrPage[j] = j + 1;
                }

                /*日期格式化*/
                // for (var i = 0; i < data.data.length; i++) {
                //     $scope.records[i].createdDate = new Date(data.data[i].createdDate).pattern("yyyy-MM-dd hh:mm:ss");
                // }
                /*改变页码颜色*/
                setTimeout(function () {
                    $('.select' + $scope.pageNum).css('color', 'red');
                }, 50);
            }else{
                if(data.totalCount==0){
                    $('#new01').hide();
                    $('#dong-bottom-page').hide();
                    $('#id-data-null').show();
                }
            }

        }, function (response) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            console.error('queryMessage error', response.status, response.statusText);
        });
    };


    /**
     * 系统消息列表查询
     * @param currentPage 当前页
     */
    $scope.SystemMsg = function (currentPage) {
        // $('#id-data-null').hide();
        $('.news-con1').hide();
        $('.news-con2').show();
        $('#yb-list1').hide();
        // $('L_newsShowMore1').hide();
        $('#yb-list2').show();
        // $('L_newsShowMore2').show();
      // $scope.system = [];
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        newsService.SystemMsg(receiveUserId, currentPage, $scope.pageSize).then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            //如果成功
            if (data.code == 1) {
                $('.select').css('color', '#666');
              if(currentPage == 1){
                $scope.system = data.data;
              }else {
                $scope.system = $scope.system.concat(data.data);
              }

              /*if($scope.system.length<10){
                  $('.L_newsShowMore2').hide();
                  $('.L_newsShowMore1').hide();
                }else if($scope.system.length>=10){
                  $('.L_newsShowMore2').show();
                  $('.L_newsShowMore1').show();
                }*/
                $scope.totalPage2 = data.totalPage;//总页数
                $scope.totalCount2 = data.totalCount;//总条数
                $scope.beginPage2 = data.beginPage;//开始页
                $scope.endPage2 = data.endPage;//结束页
                /*声明一个数组显示页数*/
                $scope.arrPage2 = new Array();
                if($scope.system == []){
                  $('#id-data-null1').css('display','block');
                  $('#id-data-null2').css('display','block');
                }else {
                  $('#id-data-null1').css('display','none');
                  $('#id-data-null2').css('display','none');
                }
                for (var j = 0; j < $scope.endPage2; j++) {
                    $scope.arrPage2[j] = j + 1;
                }
                if ($scope.totalCount2 > 0) {
                    $('#sys-bottom-page').show();
                }else{
                    $('#sys-bottom-page').hide();
                }
                /*日期格式化*/
                // for (var i = 0; i < data.data.list.length; i++) {
                //     $scope.system[i].modifieddate = new Date(data.data.list[i].modifieddate).pattern("yyyy-MM-dd hh:mm:ss");
                // }
                /*设置当前页颜色*/
                setTimeout(function () {
                    $('.select' + $scope.pageNum).css('color', 'red');
                }, 50);
            }else{
                if(data.totalCount==0){
                  $('#new02').hide();
                    $('#sys-bottom-page').hide();
                    $('#id-data-null').show();
                }
            }

        }, function (response) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            console.error('SystemMsg error', response.status, response.statusText);
        });
    };



    /**
     * 修改删除后样式
     * @param id
     */
    $scope.setDelStyle = function (id) {
        setTimeout(function () {
            $('.sdf' + id).hide();
            $('#' + id).html("已经删除");
            $('#' + id).css("background-color", '#FF0000');
            $('#' + id).css('border-color', '#FF0000');
            $('#' + id).show();
        }, 50);
    };


    /**
     * 接受消息
     * @param id 消息ID
     * @param modeType 消息类型：5--团队，4--日程，3--证据，2--任务，1--案件
     */
    $scope.acceptMsg = function (id,modelType) {
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        newsService.acceptOrRejectMsg(id, 3, $scope.userPortraitName).then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            //成功
            if (data.code == 1) {
                //如果是日程,并且已经删除。(data.data == 9表示已经被删除)
                if (modelType == 4 && data.data == 9) {
                    alert('该日程已经被删除');
                    $scope.setDelStyle(id);
                } else if (modelType == 3 && data.data == 9) {
                    alert('该证据已经被删除');
                    $scope.setDelStyle(id);
                } else if (modelType == 1 && data.data == 9) {
                    alert('该案件已经被删除');
                    $scope.setDelStyle(id);
                } else if (modelType == 2 && data.data == 9) {
                    alert('该任务已经被删除');
                    $scope.setDelStyle(id);
                } else {
                    setTimeout(function () {
                        $('.sdf' + id).hide();
                        $('#' + id).html("已经接受");
                        $('#' + id).show();
                    }, 50);
                }

            }
        }, function (response) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            console.error('acceptOrRejectMsg error', response.status, response.statusText);
        });
    };


    /**
     * 拒绝消息
     * @param id 消息ID
     * @param modelType 消息类型：4--日程，3--证据，2--任务，1--案件
     */
    $scope.rejectMsg = function (id, modelType) {
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        newsService.acceptOrRejectMsg(id, 5, $scope.userPortraitName).then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            //成功
            if (data.code == 1) {
                //如果该条消息已经被删除
                if (modelType == 4 && data.data == 9) {
                    alert('该日程已经被删除');
                    $scope.setDelStyle(id);
                } else if (modelType == 3 && data.data == 9) {
                    alert('该证据已经被删除');
                    $scope.setDelStyle(id);
                } else if (modelType == 2 && data.data == 9) {
                    alert('该任务已经被删除');
                    $scope.setDelStyle(id);
                } else if (modelType == 1 && data.data == 9) {
                    alert('该案件已经被删除');
                    $scope.setDelStyle(id);
                } else {
                    setTimeout(function () {
                        $('.sdf' + id).hide();
                        $('#' + id).html("已经拒绝");
                        $('#' + id).show();
                    }, 50);
                }

            }
        }, function (response) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            console.error('acceptOrRejectMsg error', response.status, response.statusText);
        });
    };


    /**
     * 动态消息接受或拒绝后的样式调整
     * @param ss
     */
    $scope.changeAfter = function (ss) {
        setTimeout(function () {
            for (var i = 0; i < ss.length; i++) {
                if (ss[i].status == 3) {
                    $('.sdf' + ss[i].id).hide();
                    $('#' + ss[i].id).html("已经接受");
                    $('#' + ss[i].id).css('background', '#43CD80');
                    $('#' + ss[i].id).show();

                } else if (ss[i].status == 5) {
                    $('.sdf' + ss[i].id).hide();
                    $('#' + ss[i].id).html("已经拒绝");
                    $('#' + ss[i].id).css('background', '#FF0000');
                    $('#' + ss[i].id).show();
                }else if (ss[i].status == 9) {
                    $('.sdf' + ss[i].id).hide();
                    $('#' + ss[i].id).html("已经删除");
                    $('#' + ss[i].id).css('background', '#FF0000');
                    $('#' + ss[i].id).css('border-color', '#FF0000');
                    $('#' + ss[i].id).show();
                }
            }
        }, 50);
    };


    /**
     * 用户点击分页点击改变页码-d
     */
    $scope.changePage = function (currentPage) {
        if (currentPage == undefined) {
            currentPage = 1;
        }
        if (currentPage <= $scope.totalPage && currentPage > 0) {
            $scope.queryMessage(currentPage);
            $scope.pageNum = currentPage;
        }else {
            console.log('列表页码越界!');
        }
    };


    /**
     * 用户点击分页点击改变页码-x
     */
    $scope.changePage2 = function (currentPage) {
        if (currentPage == undefined) {
            currentPage = 1;
        }
        if (currentPage <= $scope.totalPage2 && currentPage > 0) {

            $scope.SystemMsg(currentPage);
            $scope.pageNum = currentPage;
        }else {
            console.log('列表页码越界!');
        }
    };



    /**
     * 点击动态消息查看详情
     * @param itemId ID
     * @param modelType 类型 1:案件 2:任务 3:证据 4:日程
     */
    $scope.openDetails = function (itemId, modelType) {

        if (modelType == 1) {
            //需求暂时停止
        }else if (modelType == 2){
            $scope.getTaskDetail(itemId);
        }else if (modelType == 3){
            $scope.getEvidenceDetail(itemId);
        }else if (modelType == 4){
            $rootScope.loadingShow = true;
            $rootScope.fadeShow = true;
            newsService.getWorksCheduleDetail(itemId).then(function (data) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                if (data.code == 1) {
                    $scope.calendarDetails = data.data;
                    /*显示弹出框*/
                    $('#msg-calendar-details').modal('show');
                }
            }, function (response) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                console.error('getWorksCheduleDetail error', response.status, response.statusText);
            });
        }
    };

    /**
     * 点击系统消息查看详情
     * @param itemId ID
     * @param modelType 类型 1:案件 2:任务 3:证据 4:日程
     * @param statusdes 已读未读状态；
     */
    $scope.openSysDetails = function (itemId, modelType,statusdes,messageid) {

        if (modelType == 1) {
            //需求暂时停止
        }else if (modelType == 2){
            $scope.getTaskDetail(itemId);
        }else if (modelType == 3){
            $scope.getEvidenceDetail(itemId);
        }else if (modelType == 4){
            $rootScope.loadingShow = true;
            $rootScope.fadeShow = true;
            newsService.getWorksCheduleDetail(itemId).then(function (data) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                if (data.code == 1) {
                    $scope.calendarSysDetails = data.data;
                    /*显示弹出框*/
                    $('#msg-calendar-details-sys').modal('show');
                    if(statusdes=='未读'){
                        newsService.updateSysMessageStatus(messageid,3).then(function (data2) {
                            if (data2.code == 1) {
                                $scope.SystemMsg($scope.pageNum);
                            }
                        }, function (response) {
                            console.error('updateSysMessageStatus error', response.status, response.statusText);
                        });
                    }
                }
            }, function (response) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                console.error('getWorksCheduleDetail error', response.status, response.statusText);
            });
        }
    };

    /**
     * 获取任务详细
     * @param taskId  任务id
     */
    $scope.getTaskDetail = function (taskId) {
        $('.rightComeTo').animate({
            "right": "0"
        }, 300);
        $('.rightComeToZJ').animate({
            "right": "-700"
        }, 300);

        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        newsService.getTaskDetail(taskId).then(function (data) {
          if (data.code == 1) {
              $scope.detailList = data.data.task;
              $scope.plList = data.data.caseTaskReviews;
              $scope.executors = data.data.taskExecutors;
              $scope.logList = data.data.taskLogs;
              $scope.fileList = data.data.caseTaskAttses;

          }
        },function (response) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            console.error('getTaskDetail error', response.status, response.statusText);
        });
    };

    /**
     * 获取证据详情
     * @param evidenceID  证据id
     */
    $scope.getEvidenceDetail = function (evidenceID) {

        $scope.evidenceID = evidenceID;

        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        newsService.getEvidenceDetail(evidenceID).then(function (data) {
           $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;

            if (data.code == 1) {
                if (data.status == 0) {
                    alert("证据已被删除");
                }else {

                    $('.rightComeToZJ').animate({
                        "right": "0"
                    }, 300);
                    $('.rightComeTo').animate({
                        "right": "-570"
                    }, 300);

                    //获取证据评论
                    $scope.getCommentDetail(evidenceID);

                    $scope.detailData = data.data;
                    $scope.detailFJ = data.evidenceAttached;
                    $scope.detailUser = data.userlist;

                    for(var i=0;i < $scope.detailUser.length;i++){
                        if($scope.detailUser[i].icon != null){
                            $scope.detailUser[i].icon = httpservice.userHeaderImageUrl + $scope.detailUser[i].icon;
                        }
                    }
                    //修改证据类型的名字
                    var index = $scope.detailData.evidenceTypeId - 1;
                    $scope.evidenceTypeName = $scope.ETypeList[index].evidenceTypeName;

                    ////获取详情属于哪个证据组
                    //for(var i = 0;i<$scope.EGroupList.length;i++){
                    //    if($scope.detailData.evidenceGroupId == $scope.EGroupList[i].id){
                    //        $scope.detailEGName = $scope.EGroupList[i].evidenceGroupName;
                    //    }
                    //}
                }
            }
        },function (response) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            console.error('getEvidenceDetail error', response.status, response.statusText);
        });
    };

    /**
     * 获取证据类型
     */
    $scope.getEvidenceTypeList = function () {
        newsService.getEvidenceTypeList().then(function (data) {
            if (data.code == 1) {
                $scope.ETypeList = data.data;
            }
        })
    };

    // 关闭任务详情
    $('.closeDetail').click(function(){
        $(this).parents('.rightComeTo').animate({
            "right": "-570"
        }, 300);
    });

    //底部的评论之类的
    $('.pl').click(function () {
        $(this).addClass('clickActive');
        $(this).siblings().removeClass('clickActive');

        $('.plDiv').css('display','block');
        $('.rwrzDiv').css('display','none');

        $('#half2').css('display','block');
    });
    $('.rwrz').click(function () {
        $(this).addClass('clickActive');
        $(this).siblings().removeClass('clickActive');

        $('.rwrzDiv').css('display','block');
        $('.plDiv').css('display','none');

        $('#half2').css('display','block');
    });

    //点击X号关闭详情页面
    $('.closeEvidenceDetail').click(function () {
        $(this).parents('.rightComeToZJ').animate({
            "right": "-700"
        }, 300);
    });
    // 证据评论
    $scope.showPlBox = function ($event) {
        var $event = $event.target;
        var e = $($event);

        $(e).parents('.register-navTitle').siblings('#commentListId').css('display', 'block');
        $(e).parents('.register-navTitle').siblings('#logListId').css('display', 'none');
        $(e).css('border-bottom', '2px solid #00BB9C');
        $(e).siblings('#logTitleId').css('border-bottom', '0px solid #00BB9C');

        $('#zhalf2').css('display', 'block');
    }
    // 证据日志
    $scope.showRwrzBox = function ($event) {
        var $event = $event.target;
        var e = $($event);

        $(e).parents('.register-navTitle').siblings('#commentListId').css('display', 'none');
        $(e).parents('.register-navTitle').siblings('#logListId').css('display', 'block');
        $(e).css('border-bottom', '2px solid #00BB9C');
        $(e).siblings('#commentTitleId').css('border-bottom', '0px solid #00BB9C');

        $('#zhalf2').css('display', 'block');

        $scope.getLogDetail();
    };

    //获取评论
    $scope.getCommentDetail = function (evidenceID) {
        newsService.getCommentDetail(evidenceID).then(function (data) {
            if (data.code == 1) {
                $scope.plData = data.data;
            }
        });
    };
    //日志
    $scope.getLogDetail = function () {
        newsService.getLogDetail($scope.evidenceID).then(function (data) {
            if (data.code == 1) {
                $scope.rzData = data.data;
            }
        });
    };


    $scope.init();

//点击屏幕让详情消失
    $('.yb-message-one').click(function () {
        if($('.rightComeToZJ').css('right')=='0px'){

            $('.rightComeToZJ').animate({
                "right": "-700"
            }, 300);
        }
        if($('.rightComeTo').css('right')=='0px'){

            $('.rightComeTo').animate({
                "right": "-700"
            }, 300);
        }
    })

};
